package com.rongshu.view.penpal.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.rongshu.R;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.penpal.adapter.PenpalListAdapter;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.rongshu.view.penpal.present.PenpalDiscoveryPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by zhengweis on 2018/11/5.
 * description：笔友会-推荐
 */
public class Penpal_DiscoveryFragment extends BaseFragment<PenpalDiscoveryPresenter> implements INetView {
    @BindView(R.id.penpal_list)
    RecyclerView penpalList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    private List<PenpalResponse.ResponseBean> list;
    private PenpalListAdapter adapter;
    private int pageIndex = 1;
    private int pageSize = 10;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_penpal_discovery;
    }

    @Override
    protected void initViews() {
        refreshLayout.setEnableAutoLoadmore(false);

        list = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        penpalList.setLayoutManager(linearLayoutManager);
        adapter = new PenpalListAdapter(getActivity(), list);
        penpalList.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                presenter.getRecommendPenpalList(pageIndex, pageSize);
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getRecommendPenpalList(pageIndex, pageSize);
                refreshlayout.finishLoadmore(2000/*,false*/);//传入false表示刷新失败
            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new PenpalDiscoveryPresenter(getActivity()), this);
        pageIndex = 1;
        presenter.getRecommendPenpalList(pageIndex, pageSize);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
    }

    @Override
    public void success(Object object, int tag) {
        PenpalResponse response = (PenpalResponse) object;
        if (pageIndex == 1) {
            list.clear();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadmore();
        }
        if (response != null && response.getResponse() != null) {
            list.addAll(response.getResponse());
        }
        adapter.notifyDataSetChanged();
        if (response.getResponse().size() == 0) {
            refreshLayout.setLoadmoreFinished(true);
        }
        noDataLayout.setVisibility(list.size() == 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
